package com.ruin.tech.ahri.service.impl;

import com.ruin.tech.ahri.dao.UserMapper;
import com.ruin.tech.ahri.model.dto.User;
import com.ruin.tech.ahri.model.dto.UserExample;
import com.ruin.tech.ahri.model.dto.WeiXinAuthDTO;
import com.ruin.tech.ahri.model.vo.LoginInfoVO;
import com.ruin.tech.ahri.service.IUserService;
import com.ruin.tech.ahri.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 用户业务实现
 * <p>
 *
 * @author xulf
 * @date 2019-07-31 11:29
 */
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    public UserMapper userMapper;

    @Override
    public LoginInfoVO login(String loginName, String password) {
        password = Utils.digestString(password,null);
        List<LoginInfoVO> loginInfoVOS = userMapper.selectLoginInfoByLoginNameAndPassword(loginName, password);
        if(Objects.isNull(loginInfoVOS) || loginInfoVOS.size()<=0){
            return null;
        }else{
            LoginInfoVO loginInfoVO = loginInfoVOS.get(0);
            return loginInfoVO;
        }
    }

    @Override
    public void register(User user) {
        user.setPassword(Utils.digestString(user.getPassword(),null));
        userMapper.insert(user);
    }

    @Override
    public User findUserByUniqueKey(User record) {
        UserExample userExample = new UserExample();
        setUserExample(record, userExample);
        List<User> users = userMapper.selectByExample(userExample);
        if(users != null && users.size()>0){
            return users.get(0);
        }
        return null;
    }

    @Override
    public void updateUserByUniqueKey(User record) {
        UserExample userExample = new UserExample();
        userExample = setUserExample(record, userExample);
        record.setUpdateTime(new Date());
        userMapper.updateByExampleSelective(record,userExample);
    }

    @Override
    public User insertOrUpdateByOpenid(WeiXinAuthDTO weiXinAuthDTO) {
        User record = new User();
        record.setOpenId(weiXinAuthDTO.getOpenid());
        record.setUserType(0);
        record.setUpdateTime(new Date());
        record.setHeaderPath(weiXinAuthDTO.getAvatarUrl());
        record.setRoleId(2L);
        record.setGener(weiXinAuthDTO.getGender());
        record.setNickName(weiXinAuthDTO.getNickName());
        record.setSessionKey(weiXinAuthDTO.getSession_key());

        UserExample userExample = new UserExample();
        userExample = setUserExample(record,userExample);
        int size = userMapper.countByExample(userExample);
        //TODO 数据校验解密
        String sessionId = Utils.digestString(weiXinAuthDTO.getSession_key(),weiXinAuthDTO.getOpenid());
        record.setSessionId(sessionId);

        if(size > 0){
            userMapper.updateByExampleSelective(record,userExample);
        }else{
            record.setCreateTime(new Date());
            userMapper.insertSelective(record);
        }
        record = userMapper.selectByExample(userExample).get(0);
        return record;
    }

    @Override
    public User insertOrUpdateBySessionId(WeiXinAuthDTO weiXinAuthDTO) {
        User record = new User();
        record.setUserType(0);
        record.setUpdateTime(new Date());
        record.setHeaderPath(weiXinAuthDTO.getAvatarUrl());
        record.setRoleId(2L);
        record.setGener(weiXinAuthDTO.getGender());
        record.setNickName(weiXinAuthDTO.getNickName());
        record.setSessionId(weiXinAuthDTO.getSessionId());

        UserExample userExample = new UserExample();
        userExample = setUserExample(record,userExample);
        int size = userMapper.countByExample(userExample);
        //TODO 数据校验解密
        if(size > 0){
            userMapper.updateByExampleSelective(record,userExample);
            record = userMapper.selectByExample(userExample).get(0);
        }else{
            return null;
        }
        return record;
    }

    private UserExample setUserExample(User record, UserExample userExample) {
        if(record != null){
            UserExample.Criteria cri = userExample.createCriteria();
            if(record.getId() != null){
                cri.andIdEqualTo(record.getId());
            }
            if(record.getEmail() != null){
                cri.andEmailEqualTo(record.getEmail());
            }
            if(record.getMobile() != null){
                cri.andMobileEqualTo(record.getMobile());
            }
            if(record.getUserName() != null){
                cri.andUserNameEqualTo(record.getUserName());
            }
            if(record.getOpenId() != null){
                cri.andOpenIdEqualTo(record.getOpenId());
            }
            if(record.getSessionId() != null){
                cri.andSessionIdEqualTo(record.getSessionId());
            }
        }
        return userExample;
    }

}
